/*
 * Copyright (c) 2011 Imre Fazekas.
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the Brillien nor the names of its
 * terms and concepts may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.vii.streamline.services;

import java.lang.reflect.Array;
import java.util.Map;

/**
 * Service class to handle object-handling operations
 */
public class OperationServices {

    /**
	 * Checks if the parameters map contains a key with the value of the parameter 'key'.
	 * @param parameters The parameters in which to search the key.
	 * @param key The key to be found.
	 * @return True, if the map contains the key, false otherwise.
	 */
    public static boolean containsKey( Map<String, Object> parameters, String key ){
        for (String s : parameters.keySet()) {
            if( s.equalsIgnoreCase( key ) )
                return true;
        }
        return false;
    }


    /**
     * Gets the value of the 'key' parameter from the map.
     * @param parameters The parameters from from which to return the value of the key.
     * @param key The key for which to return the value.
     * @return The value of the key.
     */
    public static Object get( Map<String, Object> parameters, String key ){
        for (String s : parameters.keySet()) {
            if( s.equalsIgnoreCase( key ) )
                return parameters.get( s );
        }
        return null;
    }


    public static <T> T[] tail( T[] original, Class<T> type ){
        return OperationServices.<T>tail( original, 1, type );
    }

    public static Object[] tail( Object[] original, int startIndex ){
        return OperationServices.tail( original, 1, Object.class );
    }

    public static <T> T[] tail( T[] original, int startIndex, Class<T> type ){
        T[] res = (T[])Array.newInstance( type, original.length-startIndex );

        for( int i=startIndex; i<original.length; ++i )
            res[ i-startIndex ] = original[ i ];

        return res;
    }

    /**
     * Equals method to decide two objects' equality handling null values
     */
    public static boolean equals( Object o1, Object o2 ){
        return o1 == null ? o2 == null : o1.equals( o2 );
    }

}
